using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Colorprj.Masters;
using Colorprj.Account;
using Colorprj.Common ;
namespace Colorprj
{
    public partial class frmCashBook : Form
    {

        private string _VchType;
        private string _TrCode;
        public string TrCode
        {
            get
            {
                return _TrCode;
            }
            set
            {

                _TrCode = value;
            }
        }
        public string VchType
        {
            get
            {
                return _VchType;
            }
            set {

                _VchType = value;
            }
        }



        BuyerManager bm = new BuyerManager();
        AccountManager am = new AccountManager(); 
        public frmCashBook()
        {
            InitializeComponent();
        }

        private void frmCashBook_Load(object sender, EventArgs e)
        {
           
            setAddMode();
            AccountName.DataSource = bm.GetAllBuyers().Tables[0];
            AccountName.DisplayMember = "BuyerName";
            AccountName.ValueMember = "BuyerId"; 
        }

        private void setAddMode() {

            btnUpdate.Enabled = false;
            btnSave.Enabled = true;
            txtVNo.Text = am.GetNextVNo(VchType).ToString();
            dataGridView1.Rows.Clear();


        }

        private void setUpdMode()
        {

            btnUpdate.Enabled = true ;
            btnSave.Enabled = false;
        }


        private List<AccTransEntity> getformData()
        {
            List<AccTransEntity> dataList = new List<AccTransEntity>();
            int maxSrno = 0;
            int rowCnt = 0;
            for (rowCnt = 0; rowCnt < dataGridView1.RowCount; rowCnt++)
            {
                if (dataGridView1["PartiCulars", rowCnt].Value != null)
                {
                    AccTransEntity entity = new AccTransEntity();
                    entity.AccountId = Convert.ToInt16(dataGridView1["AccountName", rowCnt].Value);
                    entity.Amount = Convert.ToDouble(dataGridView1["amountcr", rowCnt].Value);
                    if ((entity.Amount) == 0)
                    {

                        entity.Amount = -1 * Convert.ToDouble(dataGridView1["amountdr", rowCnt].Value);
                    }
                    entity.PartiCular = dataGridView1["PartiCulars", rowCnt].Value.ToString();
                    entity.VoucherDate = dtpVoucherDate.Value;
                    entity.VoucherType = VchType;
                    entity.flg = Convert.ToInt16(dataGridView1["flg", rowCnt].Value); ;

                    if (dataGridView1["srNo", rowCnt].Value == null)
                    {
                        entity.SrNo = maxSrno + 1;
                        maxSrno++;
                    }
                    else
                    {
                        entity.SrNo = Utility.getIntVal(dataGridView1["srNo", rowCnt].Value);
                        maxSrno = entity.SrNo;
                    }
                    entity.VoucherId = Utility.getIntVal(txtVNo.Text);
                    entity.TrCode = TrCode;
                    dataList.Add(entity);
                }
                //entity.VoucherId  = Convert.ToInt16(dataGridView2["Subproduct", rowCnt].Value);
                //entity.InvoiceDetailId = Convert.ToInt16(dataGridView2["InvoiceDetId", rowCnt].Value);
                ////if (pdEntity.Qty > 0 || pdEntity.InvoiceDetailId > 0)
                ////{
                ////    pdEntityList.Add(pdEntity);
                ////}
                //////}
            }

            return dataList;
        }
        //    void setUpdMode()
        //{

        //    btnUpdate.Enabled = true;
        //    btnSave.Enabled = false;
        //}


        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            try
            {

                List<AccTransEntity> dataList = getformData();
                am.AddVoucher(dataList);
                setAddMode();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            
            }
        }

        private void btEdit_Click(object sender, EventArgs e)
        {

            frmVchNoSearch fvns = new frmVchNoSearch();
            fvns.VchType = VchType;
            if (fvns.ShowDialog() == DialogResult.OK)
            {
                int VchNo = Convert.ToInt16(fvns.ItemToRead);

                List<AccTransEntity> dataList = am.GetAccTransList(VchNo, VchType);
                dataGridView1.RowCount = dataList.Count + 1;
                int row = 0;
                foreach (AccTransEntity pdEntity in dataList)
                {
                    txtVNo.Text = pdEntity.VoucherId.ToString() ;
                    dtpVoucherDate.Value   = pdEntity.VoucherDate;

                    dataGridView1["AccountName", row].Value = pdEntity.AccountId;
                    dataGridView1["AccountName", row].ReadOnly = true;
                    if (pdEntity.Amount > 0)
                    {

                        dataGridView1["AmountCr", row].Value = pdEntity.Amount;
                    }
                    else
                    {
                        dataGridView1["AmountDr", row].Value = -1 * pdEntity.Amount;

                    }
                    dataGridView1["PartiCulars", row].Value = pdEntity.PartiCular;
                    dataGridView1["SrNo", row].Value = pdEntity.SrNo;
                    dataGridView1["flg", row].Value = 2;
                    row++;

                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            List<AccTransEntity> dataList = getformData();
            am.AddVoucher(dataList);
            setAddMode();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {

            double amountDr =Utility.getDblVal(dataGridView1.Rows[e.RowIndex].Cells["AmountDr"].Value);
            double amountCr = Utility.getDblVal(dataGridView1.Rows[e.RowIndex].Cells["AmountCr"].Value);

            //dataGridView1.AllowUserToAddRows = dataGridView1.Rows[e.RowIndex].Cells["AccountName"].Value != null;//(amountDr + amountCr) > 0  ;

            if (dataGridView1.RowCount == 0) {

                dataGridView1.AllowUserToAddRows = true;
            
            }

            //if (e.RowIndex >= 0 && (e.ColumnIndex == 2))
            //{
                
            //    double drAmount = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells["AmountDr"].Value);

            //    drAmount = Convert.ToDouble(dataGridView1[e.ColumnIndex, e.RowIndex].Value);
            //    if (drAmount == 0)
            //    {
            //        this.dataGridView1.Rows[e.RowIndex].Cells["AmountCr"].ReadOnly = false;
            //    }
            //    else {
            //        this.dataGridView1.Rows[e.RowIndex].Cells["AmountCr"].ReadOnly = true;
            //    }
            //}

            //if (e.RowIndex >= 0 && (e.ColumnIndex == 3))
            //{

            //    double crAmount = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells["AmountCr"].Value);
            //    if (crAmount == 0)
            //    {
            //        this.dataGridView1.Rows[e.RowIndex].Cells["AmountDr"].ReadOnly = false;
            //    }
            //    else
            //    {
            //        this.dataGridView1.Rows[e.RowIndex].Cells["AmountDr"].ReadOnly = true;
            //    }


            //}
            
        }

        private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && (e.ColumnIndex == 2))
            {

                double drAmount = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells["AmountDr"].Value);

                drAmount = Convert.ToDouble(dataGridView1[e.ColumnIndex, e.RowIndex].Value);
                if (drAmount == 0)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells["AmountCr"].ReadOnly = false;
                }
                else
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells["AmountCr"].ReadOnly = true;
                }
            }

            if (e.RowIndex >= 0 && (e.ColumnIndex == 3))
            {

                double crAmount = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells["AmountCr"].Value);
                if (crAmount == 0)
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells["AmountDr"].ReadOnly = false;
                }
                else
                {
                    this.dataGridView1.Rows[e.RowIndex].Cells["AmountDr"].ReadOnly = true;
                }


            } 

        }
        private bool IsDuplicate(string spvalue)
        {
            bool retval = false;
            int rowCnt = 0;
            System.Collections.Hashtable hash = new System.Collections.Hashtable();

            for (rowCnt = 0; rowCnt < dataGridView1.RowCount - 1; rowCnt++)
            {
                String SubproductId = dataGridView1["AccountName", rowCnt].FormattedValue.ToString();
                if (hash.Contains(spvalue))
                {
                    retval = true;
                    break;
                }
                else
                {
                    hash.Add(SubproductId, SubproductId);
                }

            }
            return retval;
        }
        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string headerText = dataGridView1.Columns[e.ColumnIndex].HeaderText;
            if (headerText.Equals("AccountName"))
            {
                //if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
                //{
                //    dataGridView1.Rows[e.RowIndex].ErrorText =
                //        "AccountName must not be empty";
                //    e.Cancel = true;
                //}

                if (IsDuplicate(e.FormattedValue.ToString()) && dataGridView1.Rows[e.RowIndex].Cells["flg"].Value == null)
                {
                    dataGridView1.Rows[e.RowIndex].ErrorText =
                                "AccountName Allready Exist";
                    e.Cancel = true;

                }

            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
        }

        private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
           // string  accountName = dataGridView1.Rows[e.RowIndex].Cells["AccountName"].Value;
            double amountDr =Utility.getDblVal(dataGridView1.Rows[e.RowIndex].Cells["AmountDr"].Value);
            double amountCr = Utility.getDblVal(dataGridView1.Rows[e.RowIndex].Cells["AmountCr"].Value);
            int  flg = Utility.getIntVal(dataGridView1.Rows[e.RowIndex].Cells["flg"].Value);
            if (dataGridView1.Rows[e.RowIndex].Cells["AccountName"].Value != null)
            {
                if ((amountDr + amountCr) == 0 && flg == 0)
                {
                    dataGridView1.Rows[e.RowIndex].ErrorText =
                           "Dr and Cr. both amount can not be 0";
                    e.Cancel = true;
                }
            }
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape){

        this.dataGridView1.CurrentRow.Cells["AccountName"].Value =null;

        this.dataGridView1.CancelEdit();

            }


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            setAddMode();
        }
    }
}